﻿var grid, cbProgress;

//初始化表格
function InitGrid() {
    var LICENSE_TYPE = $('#SLICENSE_TYPE').combobox('getValue');
    //var CheckTimeS = $('#CheckTimeS').datetimebox('getValue');
    //var CheckTimeE = $('#CheckTimeE').datetimebox('getValue');
    grid = $('#tab_list').datagrid({
        name: 'tbl_data',
        url: '/VenderUserLicense/Query',
        title: '供应商证书列表',
        queryParams: {
            LICENSE_TYPE: LICENSE_TYPE
            //CheckTimeS: CheckTimeS,
            //CheckTimeE: CheckTimeE
        },
        //width: 815,
        height: 530,
        //border : false,
        //fit: false,
        fitColumns: false,
        //nowrap: true,
        rownumbers: true,
        //showFooter: true,
        //loadMsg: '正在加载信息...',
        //frozen: true,
        //sortName: "CODE", //排序设置
        singleSelect: true,
        idField: 'VENDERUSER_LICENSE_ID',
        columns: [[
        //{ field: 'ck', checkbox: true },
                    { field: 'VENDERUSER_LICENSE_ID', title: 'ID', hidden: true },
                    { field: 'VENDERID', title: '供应商ID', hidden: true },
                    { field: 'VENDERNAME', title: '供应商名称', align: 'center', sortable: true },
                    { field: 'VENDERCODE', title: '供应商编码', align: 'center', sortable: true },
                    { field: 'LICENSE_TYPE', title: '证件类型', align: 'center', sortable: true, formatter: formatLicenseType },
                    { field: 'VALIDDATE', title: '有效日期', width: 150, align: 'center', sortable: true, formatter: ChangeDateFormat },
                    { field: 'IMGURL', title: '证书图片', align: 'center', sortable: true, formatter: formatimage },
                    { field: 'REMARK', title: '备注', align: 'center', sortable: true }
        ]],
        toolbar: [{
            id: 'btnAdd',
            text: '新增',
            iconCls: 'icon-add',
            handler: function () {
                add();
            }
        }, '-', {
            id: 'btnEdit',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function () {
                edit();
            }
        }, '-', {
            id: 'btnRemove',
            text: '删除',
            iconCls: 'icon-remove',
            handler: function () {
                del();
            }
        }],
        pagination: true,
        pageSize: 20,
        pageNumber: 1,
        pageList: [10, 20, 30, 40, 50],
        rownumbers: true, //行号
        onLoadSuccess: function (data) {
            onRefreshButton();
        },
        onSelect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onUnselect: function (rowIndex, rowData) {
            onRefreshButton();
        },
        onSortColumn: function (sort, order) {
            grid.datagrid('reload');
        }
    });
    //清空选择
    grid.datagrid('clearSelections');
}

function ChangeDateFormat(val) {
    if (val != null) {
        var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
        //默认时间0001/01/01设置为空
        if (date.getFullYear() == "1")
            return "";
        //月份为0-11，所以+1，月份小于10时补个0
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        return date.getFullYear() + "-" + month + "-" + currentDate;
    }

    return "";
}

function formatLicenseType(val) {
    if (val != null) {
        if (val == 1) {
            return "营业执照";
        }
        if (val == 2) {
            return "组织机构代码证";
        }
        if (val == 3) {
            return "税务登记证";
        }
        if (val == 4) {
            return "生产许可证";
        }
        if (val == 5) {
            return "食品流通许可证";
        }
    }
}

function formatimage(val, row, index) {
    if (val != null) {
        //var GoodsMoveID = row.GOODSMOVEID;
        //return "<img src='../"+val+"'/>";
        var IMGNAME = row.IMGNAME;
        return "<a href=\"javascript:showimage('" + val + "')\">" + IMGNAME + "</a>";
        //return "<a href='../" + val + "'>查看</a>";
    }
}
function showimage(val) {
    $('#p3').dialog('open').dialog('setTitle', '查看商品证书图片');
    $('#ig2').attr("src", "../" + val);
    //$('#p3').html("<img id='i2' style='height:600px;width:600px;' style src='../" + val + "'/>");
}

//控制按钮
function onRefreshButton() {
    $('#btnAdd').linkbutton('enable');
    $('#btnEdit').linkbutton('disable');
    $('#btnRemove').linkbutton('disable');

    //$('#btnImport').linkbutton('disable');
    // $('#btnExport').linkbutton('disable');

    var rows = grid.datagrid('getSelections');
    if (rows.length == 1) {
        $('#btnEdit').linkbutton('enable');
        $('#btnRemove').linkbutton('enable');
    }
}


//增加
function add() {
    $('#p1').dialog('open').dialog('setTitle', '新增供应商证书信息');
    var curr_time = new Date();
    var date = curr_time.getFullYear() + "-";
    date += curr_time.getMonth() + 1 + "-";
    date += curr_time.getDate();
    $("#ValidDate").datebox("setValue", date);
    $('#LICENSE_TYPE').combogrid("setValue", "");
    $('#VENDERID').combogrid("setValue", "");
    $("#txtRemark").val("");
    $("#FileUpload").val("");
    $("#ImgURL").val("");
    $("#ImgName").val("");
    $("#image").attr("style", "display:none");
    url = '/VenderUserLicense/Saves';
}

//修改
function edit() {
    var rows = grid.datagrid('getSelections');
    $('#p1').dialog('open').dialog('setTitle', '修改供应商证书信息');


    $("#ValidDate").datebox("setValue", ChangeDateFormat(rows[0].VALIDDATE));
    $('#LICENSE_TYPE').combogrid("setValue", rows[0].LICENSE_TYPE);
    $('#VENDERID').combogrid("setValue", rows[0].VENDERID);
    $("#txtRemark").val(rows[0].REMARK);
    $("#FileUpload").val("");
    $("#ImgURL").val(rows[0].IMGURL);
    $("#ImgName").val(rows[0].IMGNAME);
    $("#image").attr("style", "display:normal");
    $("#ShowImgName").html(rows[0].IMGNAME);
    url = '/VenderUserLicense/Saves';
}



//删除
function del() {
    var ids = [];
    var rows = grid.datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
        ids.push(rows[i].VENDERUSER_LICENSE_ID);
    }
    $.messager.confirm('提示信息', '您确认要删除吗?', function (data) {
        if (data) {
            var form = $('#venderuserlicense');
            form.form('submit', {
                url: '/VenderUserLicense/Delete' + '?ids=' + ids.join(','),
                success: function (data) {
                    eval('data=' + data);
                    if (data.success) {
                        InitGrid();
                    } else {
                        $.messager.alert('错误', data.msg, 'error');
                    }
                }
            })

        }
    });
}


function save() {
    var ValidDate = $('#ValidDate').datetimebox('getValue');
    var LICENSE_TYPE = $('#LICENSE_TYPE').combobox('getValue');
    var vid = $('#VENDERID').combobox('getValue');
    var ImgURL = $("#ImgURL").val();
    var ImgName = $("#ImgName").val();

    if (LICENSE_TYPE == null || LICENSE_TYPE == 0 || LICENSE_TYPE == "") {
        $.messager.alert('提示', '请选择证书类型', 'warning');
        return false;
    }
    if (ValidDate == "") {
        $.messager.alert('提示', '有效日期不能为空', 'warning');
        return false;
    }
    if (vid == null || vid == 0) {
        $.messager.alert('提示', '请选择供应商', 'warning');
        return false;
    }
    if (ImgURL == "" || ImgName == "") {
        $.messager.alert('提示', '请上传证书', 'warning');
        return false;
    }


    $('#venderuserlicense').form('submit', {
        url: url,
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                $('#p1').dialog('close');
                InitGrid();
            } else {
                $.messager.alert('错误', data.msg, 'error');
                //alert("保存失败！请检查");
            }
        }
    });
}


//提交参数
function submitParams() {
    $('#fm2').form('submit', {
        url: '/Distr/UpdateParams',
        success: function (data) {
            eval('data=' + data);
            if (data.success) {
                grid.datagrid('reload');
                $.messager.alert('提示', '参数修改成功!', 'info');
            } else {
                $.messager.alert('错误', data.msg, 'error');
            }
        }
    });
}

